package com.jinbai.service;

import com.jinbai.data_object.AlbumDO;
import com.jinbai.data_object.TrackDO;
import com.jinbai.repo.AlbumRepo;
import com.jinbai.repo.RelationRepo;
import com.jinbai.repo.TrackRepo;
import com.jinbai.view_object.BindView;
import com.jinbai.view_object.UserVO;

import java.util.LinkedList;
import java.util.List;

public class AlbumService {
    // 音频表  专辑表  关联表
    private final TrackRepo trackRepo = new TrackRepo();
    private final AlbumRepo albumRepo = new AlbumRepo();
    private final RelationRepo relationRepo = new RelationRepo();

    public Object getBindInfo(UserVO userVO, int aid) {
        // 根据uid,aid查询专辑表aid和title
        AlbumDO albumDO = albumRepo.selectAlbumByUidAndAid(userVO.getUid(), aid);
        if (albumDO == null) {
            throw new RuntimeException("不存在这样的专辑，请检查");
        }
        // 根据aid查询关系表，获取音频列表的所有tid
        List<Integer> tidList = relationRepo.selectTidListByAid(aid);
        List<TrackDO> trackDOList;
        // 根据tid查询track，获取所有音频的tid, title, type信息
        if (tidList.isEmpty()) {
            trackDOList = new LinkedList<>();
        } else {
            trackDOList = trackRepo.selectListByTidList(tidList);
        }
        return new BindView(userVO, albumDO, trackDOList);
    }
}
